function localDatabase() {

    let bodyElement = document.getElementsByTagName('body')[0];

    if (localStorage.getItem('index') != null)
        localStorageContent_index = localStorage.getItem('index');
    changeColorMode(localStorageContent_index);

    let localStorageContent = localStorage.getItem('notepad' + date_current + localStorageContent_index);

    let notepadElement = document.getElementById('notepad_div');
    notepadElement.value = localStorageContent === null ? "" : localStorageContent;
    timer = false;


    notepadElement.addEventListener('input', function () {
        
        clearTimeout(timer);
        timer = setTimeout(function () {
            //console.log(new Date());
            localStorage.setItem('notepad' + date_current + localStorageContent_index, notepadElement.value);
        }, 1200);

        showNum();
        
    });


    $("#notepad_div").keydown(function (e) {

        if (e.keyCode == 9) {
            insertAtCursor('    ');
            return false;
        }

    });
    

    showNum();


};

function showNum() {
    var num = "";
    var str = document.getElementById('notepad_div').value;
    str = str.replace(/\r/gi, "");
    str = str.split("\n");
    n = str.length;

    var lineobj = document.getElementById('leftNum');

    for (var i = 1; i <= n; i++) {
        if (document.all) {
            num += i + "\r\n";//判断浏览器是否是IE  
        } else {
            num += i + "\n";
        }
    }
    lineobj.value = num;
    num = "";
}
function changeColorMode(index) {
    backgroundColor = "";
    textColor = "";

    if (index == 0) {
        backgroundColor = "#FFF";
        textColor = "#000";
    }
    else if (index == 1) {
        backgroundColor = "#1BBC9B";
        textColor = "#ffffff";
    }
    else if (index == 2) {
        backgroundColor = "#0366d6";
        textColor = "#ffffff";
    }
    else if (index == 3) {
        backgroundColor = "#A77DC2";
        textColor = "#ffffff";
    }
    else if (index == 4) {
        backgroundColor = "#555";
        textColor = "#ffffff";
    }
    else if (index == 5) {
        backgroundColor = "#c8eecd";
        textColor = "#000";
    }

    localStorageContent_index = index;
    localStorage.setItem('index', index);

    clearTimeout(timer);
    document.getElementById("notepad_div").value = localStorage.getItem('notepad' + date_current + localStorageContent_index) == null ? '' : localStorage.getItem('notepad' + date_current + localStorageContent_index);


    document.getElementById("body").style["background-color"] = backgroundColor;
    document.getElementById("notepad_div").style["color"] = textColor;
    localStorage.setItem('backgroundColor', backgroundColor);
    localStorage.setItem('textColor', textColor);
    showNum();
}

function insertAtCursor(myValue) {
    myField = document.getElementById("notepad_div");
    //IE support
    if (document.selection) {
        myField.focus();
        sel = document.selection.createRange();
        sel.text = myValue;
    }
        //MOZILLA and others
    else if (myField.selectionStart || myField.selectionStart == '0') {
        var startPos = myField.selectionStart;
        var endPos = myField.selectionEnd;
        myField.value = myField.value.substring(0, startPos)
            + myValue
            + myField.value.substring(endPos, myField.value.length);
        myField.selectionStart = startPos + myValue.length;
        myField.selectionEnd = startPos + myValue.length;
    } else {
        myField.value += myValue;
    }
}
//获取当前时间，格式YYYY-MM-DD
function getNowFormatDate() {
    var date = new Date();
    var seperator1 = "-";
    var year = date.getFullYear();
    var month = date.getMonth() + 1;
    var strDate = date.getDate();
    if (month >= 1 && month <= 9) {
        month = "0" + month;
    }
    if (strDate >= 0 && strDate <= 9) {
        strDate = "0" + strDate;
    }
    var currentdate = year + seperator1 + month + seperator1 + strDate;
    return currentdate;
}
// Getting Background Colors
const bgcolor = localStorage.getItem('backgroundColor');
const color = localStorage.getItem('textColor');
if (bgcolor) document.getElementById("body").style["background-color"] = bgcolor;
if (color) document.getElementById("notepad_div").style["color"] = color;